package com.sunwayhorizo.health.hl7.persistent;

import com.sunwayhorizo.health.hl7.datatypes.common.CSImpl;
import com.sunwayhorizo.health.hl7.datatypes.common.ENXPImpl;
import com.sunwayhorizo.health.hl7.persistent.pk.RimCoreEntyNamesPK;

import java.io.Serializable;

import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;


@Entity
//@TableGenerator(name="EntityNames_IdGenerator", table="RIM_Id_Generator", pkColumnName = "pkColumn", valueColumnName = "valueColumn",pkColumnValue="Enty_Name")
//@NamedQuery(name = "EntyNames.findAll", query = "select o from EntyNames o")
@Table(name = "RIM_CORE_ENTY_NAMES")
@IdClass(RimCoreEntyNamesPK.class)
public class RimCoreEntyNames implements Serializable {
    @Id    
    @Column(name="ENTITY_ID", nullable = false, insertable = false,  updatable = false)    private Long entityId;
    @Id
    @Column(name="ENTITY_VERSION_NUM", nullable = false, insertable = false,  updatable = false)    private Long entityVersionNum;
    @Id
//    @GeneratedValue (strategy = GenerationType.TABLE,generator = "EntityNames_IdGenerator" )
    @Column(name="ENTITY_NAME_ID", nullable = false)    private Long entityNameId;    
    
    @Column(name="ENTITY_NAME")    private String formatted;
    
    @Column(name="EFFECTIVE_START_DATE_TS")    private Timestamp effectiveStartDateTs;      
    @Column(name="EFFECTIVE_END_DATE_TS")    private Timestamp effectiveEndDateTs;
     
    @Column(name="EFFECTIVE_DATE_IVL_TXT")    private String effectiveDateIvlTxt;
    @Column(name="EFFECTIVE_DATE_IVL_UOM_CODE")    private String effectiveDateIvlUomCode;
    @Column(name="EFFECTIVE_DATE_TXT")    private String effectiveDateTxt;
   
    @OneToMany(mappedBy = "rimCoreEntyNames",cascade = CascadeType.ALL,fetch = FetchType.LAZY)    
    private List<RimCoreEntyNameParts> entyNamePartsList = new ArrayList<RimCoreEntyNameParts>();
    
    @OneToMany(mappedBy = "rimCoreEntyNames",cascade = CascadeType.ALL,fetch = FetchType.LAZY)    
    private List<RimCoreEntyNameUses> entyNameUsesList = new ArrayList<RimCoreEntyNameUses>();
     
    @ManyToOne
    @JoinColumns({
    @JoinColumn(name = "ENTITY_ID", referencedColumnName = "ENTITY_ID"),
    @JoinColumn(name = "ENTITY_VERSION_NUM", referencedColumnName = "ENTITY_VERSION_NUM")
    })
    private RimCoreEntities rimCoreEntities;

    public String getEffectiveDateIvlTxt() {
        return effectiveDateIvlTxt;
    }

    public void setEffectiveDateIvlTxt(String effectiveDateIvlTxt) {
        this.effectiveDateIvlTxt = effectiveDateIvlTxt;
    }

    public String getEffectiveDateIvlUomCode() {
        return effectiveDateIvlUomCode;
    }

    public void setEffectiveDateIvlUomCode(String effectiveDateIvlUomCode) {
        this.effectiveDateIvlUomCode = effectiveDateIvlUomCode;
    }

    /**
     * @return
     */
    public String getEffectiveDateTxt() {
        return effectiveDateTxt;
    }

    /**
     * @param effectiveDateTxt
     */
    public void setEffectiveDateTxt(String effectiveDateTxt) {
        this.effectiveDateTxt = effectiveDateTxt;
    }

    public Timestamp getEffectiveEndDateTs() {
        return effectiveEndDateTs;
    }

    public void setEffectiveEndDateTs(Timestamp effectiveEndDateTs) {
        this.effectiveEndDateTs = effectiveEndDateTs;
    }

    public Timestamp getEffectiveStartDateTs() {
        return effectiveStartDateTs;
    }

    public void setEffectiveStartDateTs(Timestamp effectiveStartDateTs) {
        this.effectiveStartDateTs = effectiveStartDateTs;
    }

    private Long getEntityId() {
        return entityId;
    }

    private void setEntityId(Long entityId) {
        this.entityId = entityId;
    }

    public String getFormatted() {
        return formatted;
    }

    public void setFormatted(String formatted) {
        this.formatted = formatted;
    }

    private Long getEntityNameId() {
        return entityNameId;
    }

    private void setEntityNameId(Long entityNameId) {
        this.entityNameId = entityNameId;
    }

    private Long getEntityVersionNum() {
        return entityVersionNum;
    }

    private void setEntityVersionNum(Long entityVersionNum) {
        this.entityVersionNum = entityVersionNum;
    }

/*    private Set<EntyNameParts> getEntyNamePartsSet() {
        return entyNamePartsSet;
    }

    private void setEntyNamePartsSet(Set<EntyNameParts> entyNamePartsSet) {
        this.entyNamePartsSet = entyNamePartsSet;
    }
*/
    private RimCoreEntyNameParts addEntyNameParts(RimCoreEntyNameParts entyNameParts) {
        getEntyNamePartsList().add(entyNameParts);
        entyNameParts.setRimCoreEntyNames(this);
        return entyNameParts;
    }

    private RimCoreEntyNameParts removeEntyNameParts(RimCoreEntyNameParts entyNameParts) {
        getEntyNamePartsList().remove(entyNameParts);
        entyNameParts.setRimCoreEntyNames(null);
        return entyNameParts;
    }
    
    public void setEntyNameParts(List mList){
        for (int i = 0 ;i < mList.size() ; i++ )  {
            ENXPImpl enxp = (ENXPImpl)mList.get(i);
            RimCoreEntyNameParts entyNameParts = new RimCoreEntyNameParts();
            entyNameParts.setPartOrderNum(i);
            entyNameParts.setPartType(enxp.partType().toString());
            entyNameParts.setLiteral(enxp.literal().toString());
            this.addEntyNameParts(entyNameParts);        
        }    
    }
    
    private RimCoreEntyNameUses addEntyNameUses(RimCoreEntyNameUses entyNameUses) {
        getEntyNameUsesList().add(entyNameUses);
        entyNameUses.setRimCoreEntyNames(this);
        return entyNameUses;
    }

    private RimCoreEntyNameUses removeEntyNameUses(RimCoreEntyNameUses entyNameUses) {
        getEntyNameUsesList().remove(entyNameUses);
        entyNameUses.setRimCoreEntyNames(null);
        return entyNameUses;
    }
    
    public void setEntyNameUses(Set mSet){          
        CSImpl use = (CSImpl)mSet.iterator().next();
        RimCoreEntyNameUses entyNameUses = new RimCoreEntyNameUses();
        entyNameUses.setUse(use.code().toString());
        this.addEntyNameUses(entyNameUses);            
    }


    public void setRimCoreEntities(RimCoreEntities rimCoreEntities) {
        this.rimCoreEntities = rimCoreEntities;
    }

    public RimCoreEntities getRimCoreEntities() {
        return rimCoreEntities;
    }

    public void setEntyNamePartsList(List<RimCoreEntyNameParts> entyNamePartsList) {
        this.entyNamePartsList = entyNamePartsList;
    }

    public List<RimCoreEntyNameParts> getEntyNamePartsList() {
        return entyNamePartsList;
    }

    public void setEntyNameUsesList(List<RimCoreEntyNameUses> entyNameUsesList) {
        this.entyNameUsesList = entyNameUsesList;
    }

    public List<RimCoreEntyNameUses> getEntyNameUsesList() {
        return entyNameUsesList;
    }
}
